/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.xjbsysdetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xjbsysdetail.entity.XjbSysdetail;
import vip.xiaonuo.modular.xjbsysdetail.enums.XjbSysdetailExceptionEnum;
import vip.xiaonuo.modular.xjbsysdetail.mapper.XjbSysdetailMapper;
import vip.xiaonuo.modular.xjbsysdetail.param.XjbSysdetailParam;
import vip.xiaonuo.modular.xjbsysdetail.service.XjbSysdetailService;

import java.util.List;

/**
 * 系统信息表service接口实现类
 *
 * @author zhangping
 * @date 2022-03-18 22:57:10
 */
@Service
public class XjbSysdetailServiceImpl extends ServiceImpl<XjbSysdetailMapper, XjbSysdetail> implements XjbSysdetailService {

    @Override
    public PageResult<XjbSysdetail> page(XjbSysdetailParam xjbSysdetailParam) {
        QueryWrapper<XjbSysdetail> queryWrapper = buildQueryWrapper(xjbSysdetailParam, "page");
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XjbSysdetail> list(XjbSysdetailParam xjbSysdetailParam) {
        QueryWrapper<XjbSysdetail> queryWrapper = buildQueryWrapper(xjbSysdetailParam, "list");
        return this.getBaseMapper().selectList(queryWrapper);
    }

    private QueryWrapper<XjbSysdetail> buildQueryWrapper(XjbSysdetailParam xjbSysdetailParam, String type) {
        QueryWrapper<XjbSysdetail> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xjbSysdetailParam)) {

            // 根据系统名称 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getSysname())) {
                queryWrapper.lambda().eq(XjbSysdetail::getSysname, xjbSysdetailParam.getSysname());
            }
            // 根据系统编号 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getSysid())) {
                queryWrapper.lambda().eq(XjbSysdetail::getSysid, xjbSysdetailParam.getSysid());
            }
            // 根据渠道编号 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getChannel())) {
                queryWrapper.lambda().eq(XjbSysdetail::getChannel, xjbSysdetailParam.getChannel());
            }
            // 根据系统介绍 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getDetail())) {
                queryWrapper.lambda().eq(XjbSysdetail::getDetail, xjbSysdetailParam.getDetail());
            }
            // 根据系统提供商 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getProvider())) {
                queryWrapper.lambda().eq(XjbSysdetail::getProvider, xjbSysdetailParam.getProvider());
            }
            // 根据目前阶段 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getStage())) {
                queryWrapper.lambda().eq(XjbSysdetail::getStage, xjbSysdetailParam.getStage());
            }
            // 根据上线日期 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getUpdate())) {
                queryWrapper.lambda().eq(XjbSysdetail::getUpdate, xjbSysdetailParam.getUpdate());
            }
            // 根据运维人员 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getWorker())) {
                queryWrapper.lambda().eq(XjbSysdetail::getWorker, xjbSysdetailParam.getWorker());
            }
            // 根据研发支持人员 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getRdcWorker())) {
                queryWrapper.lambda().eq(XjbSysdetail::getRdcWorker, xjbSysdetailParam.getRdcWorker());
            }
            // 根据运营部门 查询
            if (ObjectUtil.isNotEmpty(xjbSysdetailParam.getDept())) {
                queryWrapper.lambda().eq(XjbSysdetail::getDept, xjbSysdetailParam.getDept());
            }
        }
        return queryWrapper;
    }

    @Override
    public void add(XjbSysdetailParam xjbSysdetailParam) {
        XjbSysdetail xjbSysdetail = new XjbSysdetail();
        BeanUtil.copyProperties(xjbSysdetailParam, xjbSysdetail);
        this.save(xjbSysdetail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XjbSysdetailParam> xjbSysdetailParamList) {
        xjbSysdetailParamList.forEach(xjbSysdetailParam -> {
            this.removeById(xjbSysdetailParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XjbSysdetailParam xjbSysdetailParam) {
        XjbSysdetail xjbSysdetail = this.queryXjbSysdetail(xjbSysdetailParam);
        BeanUtil.copyProperties(xjbSysdetailParam, xjbSysdetail);
        this.updateById(xjbSysdetail);
    }

    @Override
    public XjbSysdetail detail(XjbSysdetailParam xjbSysdetailParam) {
        return this.queryXjbSysdetail(xjbSysdetailParam);
    }

    /**
     * 获取系统信息表
     *
     * @author zhangping
     * @date 2022-03-18 22:57:10
     */
    private XjbSysdetail queryXjbSysdetail(XjbSysdetailParam xjbSysdetailParam) {
        XjbSysdetail xjbSysdetail = this.getById(xjbSysdetailParam.getId());
        if (ObjectUtil.isNull(xjbSysdetail)) {
            throw new ServiceException(XjbSysdetailExceptionEnum.NOT_EXIST);
        }
        return xjbSysdetail;
    }

    @Override
    public void export(XjbSysdetailParam xjbSysdetailParam) {
        List<XjbSysdetail> list = this.list(xjbSysdetailParam);
        PoiUtil.exportExcelWithStream("SnowyXjbSysdetail.xls", XjbSysdetail.class, list);
    }

}
